package com.example.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.spec.DESKeySpec;

import com.example.downloadmanager.DownloadInfo;
import com.example.ui.learn.ListeningInfo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 *
 * 一个业务类
 */
 
public class Dao {
	private Context context;
    private DBHelper dbHelper;
    private SQLiteDatabase database ;
    public Dao(Context context) {
        dbHelper = new DBHelper(context,true);
        this.context=context;
    }
    /**
     * 查看数据库中是否有数据
     */
    public synchronized boolean isHasInfors(String urlstr) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
       String sql = "select count(*)  from download_info where url=?";
        Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
        cursor.moveToFirst();
        int count = cursor.getInt(0);
       cursor.close();
        return count == 0;
    }

    /**
     * 保存 正在下载的具体信息
     */
    public synchronized void saveInfos(List<DownloadInfo> infos,ListeningInfo listen) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        for (DownloadInfo info : infos) {
            String sql = "insert into download_info(thread_id,start_pos, end_pos,compelete_size,listen_itemid ,listen_blid , "
				+ "listen_name , listen_title ,listen_id ,url ,listen_tag ,listen_thumb ,filesize) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
          Object[] bindArgs = { info.getThreadId(), info.getStartPos(),
                    info.getEndPos(), info.getCompeleteSize(),listen.getListen_itemId(), listen.getListen_blId(),
                    listen.getListen_name(),listen.getListen_title(),listen.getListen_id(), listen.getListen_uri(),listen.getListen_tag(),listen.getListen_thumb(),listen.getFileSize() };
            database.execSQL(sql, bindArgs);
        }
    }
    /**
     * 保存 还没开始下载的具体信息
     */
    public synchronized void saveListens(ListeningInfo info) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
            String sql = "insert into listen_info(listen_itemid ,listen_blid , "
				+ "listen_name , listen_title ,listen_id , listen_size ,listen_url ,listen_tag ,listen_thumb ) values (?,?,?,?,?,?,?,?,?)";
          Object[] bindArgs = { info.getListen_itemId(), info.getListen_blId(),
                    info.getListen_name(),info.getListen_title(),info.getListen_id(), info.getSize(), info.getListen_uri(),info.getListen_tag(),info.getListen_thumb() };
            database.execSQL(sql, bindArgs);
    }
    /**
     * 保存 已经下载的具体信息
     */
    public synchronized void saveListenDown(ListeningInfo info) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
            String sql = "insert into listen_down(listen_itemid ,listen_blid , "
				+ "listen_name , listen_title ,listen_id , listen_size ,listen_url ,listen_tag ,listen_thumb ) values (?,?,?,?,?,?,?,?,?)";
          Object[] bindArgs = { info.getListen_itemId(), info.getListen_blId(),
                    info.getListen_name(),info.getListen_title(),info.getListen_id(), info.getSize(), info.getListen_uri(),info.getListen_tag(),info.getListen_thumb() };
            database.execSQL(sql, bindArgs);
    }
    /**
     * 得到下载具体信息
     */
    public synchronized List<ListeningInfo> getInfos() {
        List<DownloadInfo> list = new ArrayList<DownloadInfo>();
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String sql = "select * from download_info";
        Cursor cursor = database.rawQuery(sql, null);
        List<ListeningInfo> listInfo=new ArrayList<ListeningInfo>();
        ListeningInfo info;
        while (cursor.moveToNext()) {
        	 info = new ListeningInfo(cursor.getString(10),
        			cursor.getString(12),
        			cursor.getString(8),
        			cursor.getString(7),
        			cursor.getString(6),
        			cursor.getInt(11),
        			cursor.getString(5),
        			cursor.getString(9),
        			-1,
        			cursor.getInt(4),cursor.getInt(13));
            listInfo.add(info);
        }
        cursor.close();
        return listInfo;
    }
    /**
     * 得到正在下载的具体信息
     */
    public synchronized List<DownloadInfo> getInfos(String urlstr) {
        List<DownloadInfo> list = new ArrayList<DownloadInfo>();
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String sql = "select thread_id, start_pos, end_pos,compelete_size,url from download_info where url=?";
        Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
        while (cursor.moveToNext()) {
            DownloadInfo info = new DownloadInfo(cursor.getInt(0),
                    cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),
                   cursor.getString(4));
            list.add(info);
        }
        cursor.close();
        return list;
    }
    /**
     * 查询已经下载完的数据
      */
    public synchronized List<ListeningInfo> getListen() {
        List<ListeningInfo> list = new ArrayList<ListeningInfo>();
        SQLiteDatabase database = dbHelper.getReadableDatabase();
       String sql = "select * from listen_info ";
       Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            ListeningInfo info = new ListeningInfo(cursor.getString(7),
                    cursor.getString(9), cursor.getString(4), cursor.getString(3),
                   cursor.getString(2),cursor.getInt(8),cursor.getString(1),cursor.getString(5),-1,cursor.getInt(6),cursor.getInt(10));
            list.add(info);
        }
        cursor.close();
        return list;
    }
    /**
     * 得到下载列表中还没开始下载的数据
      */
    public synchronized List<ListeningInfo> getListenDown() {
        List<ListeningInfo> list = new ArrayList<ListeningInfo>();
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        
       String sql = "select * from listen_down ";
       Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            ListeningInfo info = new ListeningInfo(cursor.getString(7),
                    cursor.getString(9), cursor.getString(4), cursor.getString(3),
                   cursor.getString(2),cursor.getInt(8),cursor.getString(1),cursor.getString(5),-1,cursor.getInt(6),cursor.getInt(10));
            list.add(info);
        }
        cursor.close();
        return list;
    }
    /**
    * 更新数据库中的正在下载的信息
     */
    public synchronized void updataInfos(int threadId, int compeleteSize, String urlstr) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String sql = "update download_info set compelete_size=? where thread_id=? and url=?";
        Object[] bindArgs = { compeleteSize, threadId, urlstr };
       database.execSQL(sql, bindArgs);
    }
    /**
     * 关闭数据库
     */  
    public void closeDb() {
        dbHelper.close();
   }
    /**
     * 删除正在下载的数据库中的数据
     */
    public synchronized void delete(String url) {
       SQLiteDatabase database = dbHelper.getReadableDatabase();
        database.delete("download_info", "url=?", new String[] { url });
        database.close();
    }
    /**
     * 删除下载完成后数据库中的数据
     */
    public synchronized void deleteListen(String url) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
         database.delete("listen_info", "listen_url=?", new String[] { url });
         database.close();
     }
    /**
     * 删除下载下载列表中还没开始下载的数据
     */
    public synchronized void deleteListenDown(String url) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();  
         database.delete("listen_down", "listen_url=?", new String[] { url });
         database.close();
     }
}
